const { contextBridge } = require('electron')
const fs = require('fs').promises;
const path = require('path');

// window.addEventListener('DOMContentLoaded', () => {
//   //todo something
// })

contextBridge.exposeInMainWorld(
  'electron',
  {
    console: () => console.log(123),
    myPromises: [Promise.resolve(), Promise.reject(new Error('whoops'))], //会报： VM75 renderer_init.js:57 Uncaught (in promise) Error: whoops 这个错误 (electron 13.1.9), 但不影响运行 //因为我们这里reject了一个promise但没有捕获
    anAsyncFunction: async () => 123,
    data: {
      myFlags: ['a', 'b', 'c'],
      bootTime: 1234
    },
    nestedAPI: {
      evenDeeper: {
        youCanDoThisAsMuchAsYouWant: {
          fn: () => ({
            returnData: 123
          })
        }
      }
    },

    //nodeapi
    async readFile(fileName){
      return fs.readFile(path.join(__dirname, fileName),"utf-8");
    },

    //ipc通信
    doThing: () => ipcRenderer.send('do-a-thing')
  }
)
